//open能力开放工程的分页列表风格的 gridList
define(function(require, exports, module) {
	//lib工具包
	require('resources/js/jquery.json-2.4.min.js');
	require('avalon2');
	
	var gridList_tpl=require('text!resources/template/common/OpenGridList_tpl.html');
	
	var OpenGridList=function($container,config){
		this.$container=$container;
		//默认值
		$.extend(true,this,OpenGridList.DEFAULT);
		//配置
		$.extend(true,this,config);
		
		//修正一些配置
		this.page.pageCountSize=parseInt(this.page.pageCountSize/2)*2+1;
		
		//拼模版
		this.$div=$(gridList_tpl).prependTo(this.$container);
		
		this.init();
	};
	
	
	OpenGridList.prototype={
		init:function(){
			this.render();
			this.bind();
			this.sync();
		},
		render:function(){
			
		},
		bind:function(){
			
		},
		getParam:function(){
			return this.vm.data.param.$model;
		},
		isSelectOne:function(){
			return $('.recordSelect:checked').length==1;
		},
		getSelectedRecords:function(){
			var records=this.vm.data.list,
				indexArray=$.map($('.recordSelect:checked'),function(ele){return +ele.value});
			return $.map(indexArray,function(index){
				return records[index].$model;
			});
		},
		load:function(param){
			if(!this.dataUrl)
				return;
			
			var that=this,data=this.vm.data;
			param&&(data.param=param);
			if(this.page.enable)
			{
				data.param.pageSize=data.page.pageSize;
				data.param.pageNo=data.page.pageNo;
			}
			$.ajax(this.dataUrl,{
				type:'POST',
				data:data.param,
				dataType:'json'
			}).done(function(data){ 
				var _data=that.vm.data;
				that.refine(data.list||[], {pageNo:data.pageNo,totalCount:data.totalCount});
				that.afterLoadData&&that.afterLoadData();
				if(_data.page.pageNo>1&&(data.list==null||data.list.length==0))
				{
					_data.page.pageNo=1;
					that.load();
				}	
			}).fail(function(){
			});
		},
		refine:function(data,page,param){
			var that=this,_data=this.vm.data;
			_data.list=data;
			_data.page||(_data.page={});
			if(this.page.enable&&page)
			{
				var pageSize=page.pageSize||_data.page.pageSize,
					pageNo=page.pageNo||_data.page.pageNo;
				
				page.pageNo=pageNo;
				page.totalCount=page.totalCount||data.length||0;
				page.totalPage||(page.totalPage=Math.ceil(page.totalCount/pageSize));
				page.nextPage||(page.nextPage=pageNo+1<page.totalPage?pageNo+1:page.totalPage);
				page.prePage||(page.prePage=pageNo-1>1?pageNo-1:1);
				//展示的控制标识
				if(page.totalPage>this.page.pageCountSize)
				{
					var halfPageCountSize=parseInt(this.page.pageCountSize/2);
					page.beginPage=pageNo-halfPageCountSize;
					page.endPage=pageNo+halfPageCountSize;
					page.hashPre=page.beginPage>1;
					if(page.beginPage<1)
					{
						page.endPage+=1-page.beginPage;
						page.beginPage=1;
					}
					page.hashNext=page.endPage<page.totalPage;
					if(page.endPage>page.totalPage)
					{
						page.beginPage+=(page.totalPage-page.endPage)
						page.endPage=page.totalPage;
					}
				}
				else
				{
					page.beginPage=1;
					page.endPage=page.totalPage;
					page.hashPre=false;
					page.hashNext=false;
				}
				page.pages=[];
				for(var ip=page.beginPage;ip<=page.endPage;ip++)
					page.pages.push(ip);
				
				_data.page.pages=[];
				_data.page=$.extend(true,_data.page.$model,page);
			}
			param&&$.extend(_data.param,param);
		},
		sync:function(){
			//使用avalon填充
			var that=this;
			this.vm = avalon.define($.extend(true,{
                $id: "gridListVM",
                selectAll:function(event){
                	$('.recordSelect',that.$div).prop('checked',$(event.target).prop('checked'));
                },
                clickRecord:function(event,index,record){
                	var $checkEle;
                	if(event.target.type!='checkbox')
                	{
                		$checkEle=$('input:checkbox',event.currentTarget);
                		$checkEle.click();
                	}
                	else
                		$checkEle=$(event.target);
                	
                	if(!$checkEle.prop('checked'))
                		$('.allselect',that.$div).prop('checked',false);
                },
                getNum:function(index){
                	return index+1+(this.data.page.pageSize*(this.data.page.pageNo-1));
                },
                getRowFieldText:function(code,column,value){
                	if(column.render)
                		return column.render(value[code],value.$model);
                	else
                		return value[code];
                },
                getNum:function(index){
                	return index+1+(this.data.page.pageSize*(this.data.page.pageNo-1));
                },
                loadPage:function(pageNo){
                	var _data=that.vm.data;
                	_data.page.pageNo=pageNo;
                	that.load();
                },
                loadNext:function(){
                	var _data=that.vm.data;
                	_data.page.pageNo=_data.page.nextPage;
                	that.load();
                },
                loadPre:function(){
                	var _data=that.vm.data;
                	_data.page.pageNo=_data.page.prePage;
                	that.load();
                }
            },this));//联合资源的vm
			avalon.scan(this.$div.get(0),this.vm);
			this.load();
		}
	};
	
	OpenGridList.DEFAULT={
			data:{
				list:[],
				param:{
					pageSize:undefined,
					pageNo:undefined
				},
				page:{
					totalPage:0,
					totalCount:0,
					pageSize:10,
					pageNo:1,
					nextPage:1,
					prePage:1
				}
			},
			dataUrl:'',
			checkOpt:true,
			radioOpt:false,
			/**columns:{//列信息
			 	'code':{
			 		title:'',
			 		render:function(value,record){}
			 	}
		 	}*/
			columns:{},
			numColumn:true,
			keyColumn:'',	//主键列
			page:{
				enable:true,
				pageSize:10,
				mini:false,
				pageCountSize:5
			},	
			afterLoadData:function(){},
			onClickRecord:function(){}
		};
	
	return OpenGridList;
});